package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	var timeout chan struct{} = make(chan struct{})
	var result chan int = make(chan int)

	// 超时后写入数据到timeout管道
	go func() {
		time.Sleep(time.Second * 5)
		timeout <- struct{}{}
	}()
	go func() {
		rand.Seed(time.Now().Unix()) // 随机数种子
		interval := time.Duration(rand.Intn(10)) * time.Second
		fmt.Println(interval)
		time.Sleep(interval)
		result <- 1
	}()
	select {
	case <-timeout: // 超时执行相对应代码
		fmt.Println("timeout")
	case <-result:
		fmt.Println("ok")

	}
}
